package com.hd.pzhwlxt.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hd.pzhwlxt.pojo.Company;
import com.hd.pzhwlxt.service.CompanyService;
import com.hd.pzhwlxt.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 公司信息模块
 */

@RestController
@RequestMapping("/company")
public class CompanyController {
    @Autowired
    private CompanyService companyService;

    @GetMapping("/getall")
    public Response<List<Company>> getAll(){
        return Response.createSuccessResponse("查询成功",companyService.list());
    }

    @GetMapping("/get")
    public Response<Page<Company>> getAllStaff(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",defaultValue = "15") Integer pageSize){
        Page<Company> companys = companyService.page(new Page<>(pageNum,pageSize));
        if(companys!=null){
            return Response.createSuccessResponse("查询成功", companys);
        }
        return Response.createErrorResponse("查询失败");
    }

    @GetMapping("/get/{id}")
    public Response<Company> getCompanyById(@PathVariable Integer id){
        Company company = companyService.getById(id);
        if(company!=null){
            return Response.createSuccessResponse("查询成功", company);
        }
        return Response.createErrorResponse("查询失败");
    }

    @PostMapping("/add")
    public Response<String> addCompany(@RequestBody Company company){
        System.out.println(company);
        boolean save = companyService.save(company);
        if (save){
            return Response.createSuccessResponse("添加成功");
        }
        return Response.createErrorResponse("添加失败");
    }

    @DeleteMapping("/delmore/{ids}")
    public Response<String> deleteCompanyByIds(@PathVariable Integer[] ids){
        boolean remove = companyService.removeByIds(Arrays.asList(ids));
        if (remove){
            return Response.createSuccessResponse("删除成功");
        }
        return Response.createErrorResponse("删除失败");
    }

    @PutMapping("/update")
    public Response<String> updateCompany(@RequestBody Company companyBasic){
        boolean update = companyService.updateById(companyBasic);
        if (update){
            return Response.createSuccessResponse("修改成功");
        }
        return Response.createErrorResponse("修改失败");
    }

}
